package com.polaris.live.ui.me

import android.os.Bundle
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.polaris.live.adapter.chatPrice.ChatLevelPriceAdapter
import com.polaris.live.common.R.color
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.ext.toLevel
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityMyChatPriceBinding
import com.polaris.live.popup.SetPricePopup
import com.polaris.live.resp.back_resp.CallPriceResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.me.MeViewModel

/**
 * 我的聊天价格
 *
 * @author Created by lucas on 2023/11/07 19:52
 * @since 1.0.0
 */
class MyChatPriceActivity : BaseVbActivity<MeViewModel, ActivityMyChatPriceBinding>() {

    private val mAdapter by lazy { ChatLevelPriceAdapter() }

    private var mCallPriceResp: CallPriceResp? = null

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.bvTitle)
        setStatusBarDarkFont(false)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.recyclerView.adapter = mAdapter
        mBinding.tvPriceMin.text = "/${StringUtils.getString(string.min)}"
        initListener()

        initObserver()

        mViewModel.getCallPrice()
    }

    private fun initListener() {
        mBinding.tvLevel.setOnSingleClickListener {
            Router.builder(RoutePath.MY_ANCHOR_LEVEL_ACTIVITY)
                .navigation(this)
        }
        mBinding.tvMeLevel.setOnSingleClickListener {
            Router.builder(RoutePath.MY_ANCHOR_LEVEL_ACTIVITY)
                .navigation(this)
        }
        mBinding.ivEdit.setOnSingleClickListener {
            mCallPriceResp?.priceList?.let {
                XPopup.Builder(this)
                    .isDestroyOnDismiss(true)
                    .asCustom(
                        SetPricePopup(
                            this,
                            mCallPriceResp?.price ?: 0L,
                            mCallPriceResp?.level ?: 0L,
                            mCallPriceResp?.minLevel ?: 0L,
                            it,
                            priceCallBack = { price ->
                                mViewModel.modifyCallPrice(price)
                            })
                    )
                    .show()
            }
        }
    }

    private fun initObserver() {
        mViewModel.mCallPriceLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mCallPriceResp = it
                val span = SpanUtils.with(mBinding.tvChatPriceTip)
                    .append(" ${StringUtils.getString(string.me_chat_price_tip)}")
                    .append(" ${StringUtils.getString(string.me_chat_price_tip_level, it.minLevel.toLevel)}")
                    .setForegroundColor(ColorUtils.getColor(color.color_57575C))
                    .create()
                mBinding.tvChatPriceTip.text = span
                mBinding.tvLevel.setLevel(it.level)
                mBinding.tvMeLevel.setLevel(it.level)
                mBinding.tvPrice.text = it.price.toString()
                mAdapter.setModifyLevel(it.minLevel.toInt())
                mAdapter.setList(it.ruleList)
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }

        mViewModel.mModifyCallPriceLiveData.observe(this) {
            mViewModel.getCallPrice()
        }
    }
}